﻿using Rbac.Domain.Menus;
using Rbac.Repository.Menus;
using Rbac.Services.Menus.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Rbac.Services.Menus
{
    public class MenuService : IMenuService
    {
        private readonly IMenuRepository repository;

        public MenuService(IMenuRepository repository)
        {
            this.repository = repository;
        }

        public List<MenuDto> Query()
        {
            var list = repository.QueryAll().Select(m => new MenuDto {
                ComponentPath = m.ComponentPath,
                Icon = m.Icon,
                MenuCode = m.MenuCode,
                MenuName = m.MenuName,
                MenuType = m.MenuType,
                MenuId = m.MenuId,
                ParentId = m.ParentId,
                RoutePath = m.RoutePath,
                ParentPath = m.ParentPath
            }).ToList();

            var rootNode = list.Where(m => m.ParentId == 0).ToList();
            foreach (var item in rootNode)
            {
                Recursion(item, list);
            }

            return rootNode;
        }

        /// <summary>
        /// 递归方法
        /// </summary>
        /// <param name="dto">当前要查询子节点的节点</param>
        /// <param name="list">所有的节点信息</param>
        public void Recursion(MenuDto dto, List<MenuDto> list)
        {
            var subNode = list.Where(m => m.ParentId == dto.MenuId).ToList();
            if(subNode.Count() > 0)
            { 
                dto.Children = new List<MenuDto>();
                foreach (var item in subNode)
                {   
                    dto.Children.Add(item);
                    Recursion(item, list);
                }
            }
        }

        public ResultDto Create(CreateMenuDto menu)
        {
            repository.Create(new Menu {
                ComponentPath = menu.ComponentPath,
                CreateTime = DateTime.Now,
                MenuCode = menu.MenuCode,
                MenuName = menu.MenuName,
                MenuType = menu.MenuType,
                ParentId = menu.ParentId,
                RoutePath = menu.RoutePath,
                ParentPath = menu.ParentPath
            });

            return new ResultDto { Code = State.Success };
        }

        public ResultDto Update(UpdateMenuDto menu)
        {
            var entity = repository.Query(menu.MenuId);
            entity.RoutePath = menu.RoutePath;
            entity.ParentPath = menu.ParentPath;
            entity.ComponentPath = menu.ComponentPath;
            entity.ParentId = menu.ParentId;
            entity.MenuCode = menu.MenuCode;
            entity.MenuName = menu.MenuName;
            entity.MenuType = menu.MenuType;

            repository.Update(entity);

            return new ResultDto { Code = State.Success };
        }

        public ResultDto Delete(int Id)
        {
            //判断当前的Id有没有子级
            var subMenu = repository.Query().Any(s => s.ParentId == Id);
            if (subMenu)
            {
                return new ResultDto { Code = State.Failed, Msg = "不能删除，还有子级菜单" };
            }
            try
            {
                repository.Delete(Id);
                return new ResultDto { Code = State.Success };
            }
            catch (Exception e)
            {
                return new ResultDto { Code = State.Failed, Msg = e.Message };
            }
        }
    }
}
